﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace SRF.UI.View
{
    public static class ControllerExtensions
    {
        public static TreeJsonResult<T> TreeJson<T>(this Controller controller, IList<T> list, Func<T, string> getId, Func<T, string> getPId, Func<T, string> getName, Action<T, IDictionary<string, object>> getAttrs = null)
        {
            return new TreeJsonResult<T>(list, getId, getPId, getName, getAttrs);
        }
        public static ExceptionResult Exception(this Controller controller, string message, int statusCode)
        {
            return new ExceptionResult(message, statusCode);
        }
        /// <summary>
        /// 跳转到主页
        /// </summary>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static RedirectResult RedirectToHome(this Controller controller)
        {
            var url = "";
            if (MvcSiteMapProvider.SiteMaps.Current != null)
            {
                var rootNode = MvcSiteMapProvider.SiteMaps.Current.RootNode;
                url = rootNode.Url;
            }

            return new RedirectResult(url);
        }
    }
}